/**
 * 
 */
package com.aspire.android.heliar.pub.render;

import java.util.Map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ImageSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.aspire.android.heliar.Heliar;
import com.aspire.android.heliar.R;
import com.aspire.android.heliar.pub.ContentNode;
import com.aspire.android.heliar.pub.ImageAttachment;
import com.aspire.android.heliar.pub.Pub;
import com.aspire.android.heliar.pub.PubAttachment;
import com.aspire.android.heliar.utils.ContextFactory;

/**
 * @author panglei126
 * 
 */
public class OutlineFrameRender extends PubRender2 {

	public static final String PUB_TITLE = "<u>%s</u>";
	public static final String PUB_TITLE_PARAGRAPH_SEP = "----------------------------------";
	public static final String PUB_PARAGRAPH_SEP = "<br/>";
	public static final String PUB_IMAGE = "pub_img";
	public static final String PUB_BACK_RETURN = "\n";
	public static final String PUB_CONTENT_NODE_SEP = "----------------------------------<br/>";

	public static final String PUB_PUB_TITLE = "<H2>%s</H2>";
	public static final String PUB_COLUMN_TITLE = "<H1>%s</H1>";

	public OutlineFrameRender(View parentView) {
		super(parentView);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.aspire.android.heliar.pub.render.PubRender2#render(com.aspire.android
	 * .heliar.pub.Pub)
	 */
	@Override
	public void render(Pub pub) {

		TextView textView = new TextView(ContextFactory.getInstance());
		textView.setTextSize(20f);
		textView.setText(Html.fromHtml(String.format(PUB_PUB_TITLE,
				pub.getTitle())));
		LinearLayout view = (LinearLayout) this.getParentView();
		view.addView(textView);

		outlines.put(pub.getTitle(), textView.getHeight());

		renderContentNode(pub.getRootNode());
	}

	private void renderContentNode(ContentNode node) {

		SpannableStringBuilder builder = new SpannableStringBuilder();

		if (node.getTitle() != null) {
			builder.append(Html.fromHtml(String.format(PUB_COLUMN_TITLE,
					node.getTitle())));
		}

		boolean isContentNodeTool = true;
		if (node.getContent() != null) {
			builder.append(Html.fromHtml(node.getContent().replaceAll(
					PUB_BACK_RETURN, PUB_PARAGRAPH_SEP)));
		} else {
			isContentNodeTool = false;
		}

		for (PubAttachment attachment : node.getAttachments()) {
			renderAttachment(attachment, builder);
			builder.append(Html.fromHtml(PUB_PARAGRAPH_SEP));

		}

		LinearLayout view = (LinearLayout) this.getParentView();
		outlines.put(node.getTitle(), view.getHeight());

		View contentNodeView = getContentView(node, builder, isContentNodeTool);
		view.addView(contentNodeView);

		if (node.getChildNodes().size() > 0) {
			for (ContentNode childNode : node.getChildNodes()) {
				renderContentNode(childNode);
			}
		}

	}

	private View getContentView(final ContentNode contentNode,
			SpannableStringBuilder builder, boolean isToolbar) {
		LayoutInflater inflater = (LayoutInflater) ContextFactory.getInstance()
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View layout = inflater.inflate(R.layout.content_node, null);
		TextView content = (TextView) layout
				.findViewById(R.id.contentNodeContent);

		/*
		View toolbar = layout.findViewById(R.id.contentNodeToolbar);
		if (!isToolbar) {
			toolbar.setVisibility(View.INVISIBLE);
		}

		ImageView shareButton = (ImageView) layout.findViewById(R.id.btnShare);
		shareButton.setOnClickListener(new ContentNodeShareButtonClickListener(
				(Activity) ContextFactory.getInstance(), contentNode));
		*/

		content.setText(builder);
		return layout;
	}

	private int renderAttachment(PubAttachment attachment,
			SpannableStringBuilder builder) {
		int attachmentHeight = 0;
		if (attachment instanceof ImageAttachment) {
			Log.d(Heliar.LOG_TAG, "add attachment");
			ImageAttachment imageAttachment = (ImageAttachment) attachment;

			Bitmap bitmap = null;
			if (imageAttachment.getBitmap() != null) {
				bitmap = imageAttachment.getBitmap();
			} else {
				bitmap = BitmapFactory.decodeFile(attachment.getFilepath());
			}

			// to get attachment height
			attachmentHeight = bitmap.getHeight();

			ImageSpan span = new ImageSpan(bitmap, ImageSpan.ALIGN_BASELINE);
			SpannableStringBuilder imagebuilder = new SpannableStringBuilder(
					PUB_IMAGE);

			imagebuilder.setSpan(span, 0, PUB_IMAGE.length(),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
			builder.append(imagebuilder);
		}

		return attachmentHeight;
	}

	@Override
	public Map<String, Integer> getOutLines() {

		int scrollOffSet = 0;
		int i = 0;

		LinearLayout view = (LinearLayout) this.getParentView();

		for (String key : outlines.keySet()) {
			outlines.put(key, scrollOffSet);
			View contentView = (View) view.getChildAt(i);
			scrollOffSet += contentView.getMeasuredHeight();
			i++;
		}

		return outlines;
	}

}
